<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use EasyWeChat\Foundation\Application;

class UserController extends Controller
{
  public $app = '';
  public function __construct(){
      $options = [
          'debug'  => true,
          'app_id' => 'wx05a1fddf4522707b',
          'secret' => '836c97f4f14b9c83cee3047f1948eee2',
          'token'  => 'hongwei',
          // 'aes_key' => null, // 可选
          'log' => [
              'level' => 'debug',
              'file'  => public_path('/log/easywechat.log'), // XXX: 绝对路径！！！！
          ],
          //...
          'oauth' => [
              'scopes'   => ['snsapi_userinfo'],
              'callback' => '/login',
          ],
      ];
      $this->app = new Application($options);
  }

  public function center(Request $req){

      if(!$req->session()->has('uinfo')){
          $oauth = $this->app->oauth;
          return $oauth->redirect(); //去找微信授权跳转到login
      }
      return '您好,欢迎登陆';
  }

  public function login(){
      $oauth = $this->app->oauth;
      $user = $oauth->user();   //微信提供给我们用户信息
      session()->put('uinfo',$user);  //把用户信息写入session

      return redirect('center');
  }
  //商城不用退出,此处仅用于调试
  public function logout(){
      session()->forget('uinfo');
  }
}
